package com.edais.actions;

import com.alibaba.fastjson.JSONObject;
import com.edais.domain.advert.UserSignManage;
import com.edais.service.SystemSettingService;
import com.edais.service.advert.BannerManageService;
import com.edais.service.advert.SignManageService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;
import com.edais.vo.BannerManageVo;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultConstant;
import com.yunzhi.domain.ResultDTO;
import com.yunzhi.util.RequestUtil;
import com.yunzhi.util.ResponseUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 签到管理 Action
 * 
 * @author wuhan
 *
 */
@Controller
@RequestMapping("/signManage")
public class SignManageAction implements Serializable {

	private static final long serialVersionUID = -9142247159133431420L;

	private static final Log logger = LogFactory.getLog(SignManageAction.class);

	@Autowired
	private SignManageService signManageService;

	@Autowired
	private BannerManageService bannerManageService;
	
	@Autowired
	private SystemSettingService systemSettingService;

	/**
	 * 查询签到管理表信息
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/querySign")
	public void querySign(HttpServletRequest request, HttpServletResponse response) {
		String logInfo = "querySign ";
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = null;
		logger.info(logInfo + "开始");
		
		try {
			String domian=systemSettingService.getSystemSettingValueByName("domain_name");
			UserSignManage userSignManage = signManageService.querySignInfo();
			if (userSignManage != null) {
				resultMap.put("status", userSignManage.getStatus());
				resultMap.put("isSign", userSignManage.getIsSign());
				resultMap.put("appPageUrl", userSignManage.getAppPageUrl());
				resultMap.put("wapUrl",CommonUtil.getDomianUrl(domian, userSignManage.getWapUrl()));
				resultMap.put("webUrl", userSignManage.getWebUrl());
				resultMap.put("intro", userSignManage.getIntro());
				resultMap.put("title", userSignManage.getTitle());
			} else {
				// 如果数据表无数据，走默认签到流程
				resultMap.put("status", "Y");
				resultMap.put("isSign", "Y");
				resultMap.put("appPageUrl", "");
				resultMap.put("wapUrl", "");
				resultMap.put("webUrl", "");
				resultMap.put("intro", "");
				resultMap.put("title", "");
			}
			resultMap.put("rescode", Consts.SUCCESS_CODE);
			resultMap.put("resmsg", Consts.SUCCESS_CN);
			resultMap.put("resmsg_cn", Consts.SUCCESS_CN);
			result = JsonUtil.getJsonStringFromMap(resultMap);
			logger.info(logInfo + " " + result);
			logger.info(logInfo + "结束");

			CommonUtil.responseJson(result, response);

		} catch (Exception e) {
			logger.error(logInfo + "异常", e);
			result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001, Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
			logger.info(logInfo + "结束");
			return;
		}
	}

	/**
	 * 查询广告banner接口
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/queryAdBanner")
	public void queryAdBanner(HttpServletRequest request, HttpServletResponse response) {

		String logInfo = "queryAdBanner ";

		logger.info(logInfo + "开始");


		try {

			if (!RequestUtil.validSign(request)) {
				logger.info("无效签名");
				ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
				return;
			}
			JSONObject params = RequestUtil.decodeDataJson(request);
			if(null == params){
				logger.info(logInfo + "参数获取失败");
				ResponseUtil.write(response,new ResultDTO(ResultCode.FAILED,
						"参数获取失败"));
				return;
			}
			String type = params.getString("type");
			if (StringUtils.isBlank(type)) {
				logger.info(logInfo + "type is null");
				ResponseUtil.write(response,new ResultDTO(ResultCode.FAILED,
						"参数type为空"));
			}
			ResultDTO<BannerManageVo> resultDTO = bannerManageService.queryBannerList(params);
			if (null == resultDTO || resultDTO.codeEq(ResultCode.FAILED)) {
				logger.error(logInfo + "查询失败");
				ResponseUtil.write(response,resultDTO);
				return;
			}

			//广告只取第一个
			List<BannerManageVo> voList = resultDTO.getRecords();
			resultDTO.setRecord(voList.get(0));
			resultDTO.setRecords(new ArrayList<>());
			resultDTO.setTotal(0);
			logger.info(logInfo + "查询结果:" + resultDTO.toJsonString());
			ResponseUtil.write(response,resultDTO);
			logger.info(logInfo + "结束");
		} catch (Exception e) {
			logger.error(logInfo + "异常", e);
			ResponseUtil.write(response,new ResultDTO(ResultCode.FAILED));
		}
	}
}
